package com.erdos.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erdos.common.service.BaseCRUDService;
import com.erdos.core.dao.FacilityDao;
import com.erdos.core.entity.Facility;
import com.erdos.core.service.FacilityService;

public class FacilityServiceImpl extends BaseCRUDService<Facility> implements
        FacilityService {

    private FacilityDao facilityDao;

    public void setFacilityDao(FacilityDao facilityDao) {
        this.facilityDao = facilityDao;
    }

    /* (non-Javadoc)
     * @see com.hp.core.service.FacilityService#findFacilityValueMap(java.lang.String)
     */
    public Map<String, String> findFacilityValueMap() {
        List<Facility> facs = this.facilityDao.findAll();
        Map<String, String> results = new HashMap<String, String>();
        for (Facility f : facs) {
            results.put(f.getFacilityId(), f.getFacilityDesc());
        }
        return results;
    }

    /* (non-Javadoc)
     * @see com.hp.core.service.FacilityService#findByFacilityId(java.lang.String)
     */
    public List<Facility> findByFacilityIds(String facilityId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("facilityId", facilityId);
        return (List<Facility>) facilityDao.findByParams(params);
    }

    /* (non-Javadoc)
    * @see com.hp.core.service.FacilityService#findByFacilityId(java.lang.String)
    */
    public Facility findByFacilityId(String facilityId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("facilityId", facilityId);
        return facilityDao.findUniqueByParams(params);
    }

    public String findByFacilityids(String facilityIds) {
        if (null == facilityIds)
            return null;
        if ("".equals(facilityIds.trim()))
            return null;
        String[] facilityArray = facilityIds.split("\\,");

        String result = null;
        for (String facilityId : facilityArray) {
            Facility facility = findByFacilityId(facilityId);
            if (null != facility) {
                if (null == result)
                    result = facility.getL4FacilityId();
                else
                    result = result + "," + facility.getL4FacilityId();
            }
        }
        return result;
    }
}
